package logic

import (
	"context"
	"usershop/api/internal/svc"
	"usershop/api/internal/types"
	"usershop/api/pkg"
	"usershop/user_srv/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginUserLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLoginUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginUserLogic {
	return &LoginUserLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *LoginUserLogic) LoginUser(req *types.RegisterShop) (resp *types.ResMsg, err error) {
	// todo: 用户密码登录

	if !pkg.UsernameFormat(req.Username) {
		return &types.ResMsg{
			Cre:  -2,
			Msg:  "账户或密码格式不正确",
			Data: nil,
		}, nil
	}

	//pwd := pkg.Md5String(req.Password) //密码解密

	res, err := l.svcCtx.UserSrv.GetUserInfoByUsername(l.ctx, &user.GetUserInfoByUsernameRequest{Username: req.Username})

	if err != nil {
		return &types.ResMsg{
			Cre:  -1,
			Msg:  "服务器内部错误",
			Data: nil,
		}, nil
	}

	if res.UserId == 0 {
		return &types.ResMsg{
			Cre:  -2,
			Msg:  "用户名不存在",
			Data: nil,
		}, nil
	}

	return &types.ResMsg{
		Cre:  0,
		Msg:  "登录成功",
		Data: nil,
	}, nil

}
